﻿@{
    ViewData["Title"] = "任务管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">

            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">任务名称：</label>
                        <div class="layui-input-inline mr0">
                            <input id="taskName" name="taskName" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">组名称：</label>
                        <div class="layui-input-inline mr0">
                            <input id="groupName" name="groupName" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">创建时间：</label>
                        <div class="layui-input-inline mr0">
                            <input id="createTime" name="createTime" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">调用方式：</label>
                        <div class="layui-input-inline mr0">
                            <select name="method">
                                <option value="">全部</option>
                                <option value="get">Get</option>
                                <option value="post">Post</option>
                                <option value="put">Put</option>
                                <option value="delete">Delete</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <a class="layui-btn icon-btn" id="addTask">
                            <i class="layui-icon">&#xe654;</i>新增定时任务
                        </a>
                        <a class="layui-btn icon-btn" href="https://cron.qqe2.com" target="_blank">
                            Cron生成器
                        </a>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="send">手动调用</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelAuth">
    <form id="TaskEditForm" lay-filter="TaskEditForm" class="layui-form model-form">
        <input type="hidden" name="guid" id="guid">
        <div class="layui-form-item layui-row">
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">任务名称</label>
                <div class="layui-input-block">
                    <input name="taskName" placeholder="请输入任务名称" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">组名称</label>
                <div class="layui-input-block">
                    <input name="groupName" placeholder="请输入组名称" type="text" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">时间配置(Cron)</label>
                <div class="layui-input-block">
                    <input name="taskCron" placeholder="请输入组名称" type="text" class="layui-input" />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">接口地址</label>
                <div class="layui-input-block">
                    <input name="taskUrl" placeholder="请输入接口地址" type="text" class="layui-input"
                           lay-verType="tips" lay-verify="required" required />
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">调用方式</label>
                <div class="layui-input-block">
                    <select name="method" lay-verType="tips" lay-verify="required" required>
                        <option value="get">Get</option>
                        <option value="post">Post</option>
                        <option value="put">Put</option>
                        <option value="delete">Delete</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">请求参数(json)</label>
                <div class="layui-input-block">
                    <textarea name="requestBody" id="requestBody" class="layui-textarea" style="height:90px;"></textarea>
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">参数类型</label>
                <div class="layui-input-block">
                    <select name="bodyType" lay-verType="tips" lay-verify="required" required>
                        <option value="1">FromBody</option>
                        <option value="2">FromForm</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">验证方式</label>
                <div class="layui-input-block">
                    <select name="authType" id="authType" lay-filter="authType" lay-verType="tips" lay-verify="required" required>
                        <option value="0">无需验证</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md5">
                <label class="layui-form-label">是否提醒</label>
                <div class="layui-input-block">
                    <select name="isTip" lay-filter="isTip" lay-verType="tips" lay-verify="required" required>
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-col-md5 EmailTip" style="display:none">
                <label class="layui-form-label">错误次数</label>
                <div class="layui-input-block">
                    <input name="errorNumTip" placeholder="当发生多少次错误后进行邮件提醒" value="5" type="text" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')" />
                </div>
            </div>
            <div class="layui-inline layui-col-md5 EmailTip" style="display:none">
                <label class="layui-form-label">最多提醒次数</label>
                <div class="layui-input-block">
                    <input name="maxTipNum" placeholder="请输入最多提醒次数" value="1" type="text" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')" />
                </div>
            </div>
            <div class="layui-inline layui-col-md12 headerAuth">
                <label class="layui-form-label">请求头(json)</label>
                <div class="layui-input-block">
                    <textarea name="token" id="token" class="layui-textarea" style="height:90px;"></textarea>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="saveTask" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/html" id="tableIsOpen">
    <input type="checkbox" lay-filter="openTask" value="{{d.guid}}" lay-skin="switch"
           lay-text="开启|关闭" {{d.isOpen==1?'checked':''}} />
</script>
<!-- js部分 -->
<script>
    layui.use(['admin', 'layer', 'form', 'table', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        form.render('select');
        // 渲染表格
        var insTb = table.render({
            elem: '#tableTbBas',
            url: '/api/Quartz',
            page: true,
            limit: 20,
            cellMinWidth: 100,
            cols: [[
                { field: 'taskName', align: 'center', title: '任务名称' },
                { field: 'groupName', align: 'center', title: '组名称' },
                { field: 'taskCron', align: 'center', title: '时间配置', width: '8%' },
                { field: 'taskUrl', align: 'center', title: '接口地址', width: '16%' },
                { field: 'method', align: 'center', title: '请求方式' },
                { field: 'requestBody', align: 'center', title: '请求参数', width: '10%' },
                {
                    field: 'bodyType', align: 'center', title: '参数形式', templet: function (d) {
                        if (d.bodyType == 1) {
                            return 'FromBody';
                        } else if (d.bodyType == 2) {
                            return 'FromForm';
                        }
                    }
                },
                {
                    field: 'authName', align: 'center', title: '验证方式', width: '10%'
                },
                { templet: '#tableIsOpen', align: 'center', title: '是否开启', },
                { field: 'createTime', align: 'center', title: '创建时间' },
                { templet: '#tableBar', title: '操作', align: 'center', minWidth: 160 }
            ]]
        });

        //监听工具条
        table.on('tool(tableTbBas)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == "edit") {
                admin.open({
                    area: ["900px", "500px"],
                    type: 1,
                    title: '修改任务[' + data.taskName + ']',
                    content: $('#modelAuth').html(),
                    success: function (layero, dIndex) {
                        QuaryAuth();
                        form.val('TaskEditForm', data);  // 回显数据
                        if (data.isTip == 1) {
                            $(".EmailTip").fadeIn();
                        }
                        form.on('select(isTip)', function (select) {
                            if (select.value == 0) {
                                $(".EmailTip").fadeOut();
                            } else if (select.value == 1) {
                                $(".EmailTip").fadeIn();
                            }
                        });
                        form.render('select');
                        // 表单提交事件
                        form.on('submit(saveTask)', function (rowData) {
                            layer.load(2);
                            $.ajax({
                                url: '/api/Quartz/' + data.guid,
                                type: 'put',
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify(rowData.field),
                                dataType: "json",
                                complete: function (obj) {
                                    layer.closeAll('loading');
                                },
                                success: function (res) {
                                    if (res.success) {
                                        layer.msg(res.message, { icon: 1 });
                                        insTb.reload({}, 'data');
                                        layer.close(dIndex);
                                    } else {
                                        layer.msg(res.message, { icon: 2 });
                                    }
                                }
                            });
                            return false;
                        });
                    }
                });
            } else if (layEvent == "del") {
                layer.confirm('确定要删除“' + data.taskName + '”任务吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.load(2);
                    $.ajax({
                        url: '/api/Quartz/' + data.guid,
                        type: 'delete',
                        contentType: "application/json;charset=utf-8",
                        async: true,
                        dataType: 'json',
                        complete: function (obj) {
                            layer.closeAll('loading');
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result.success) {
                                layer.msg(result.message, { icon: 1 });
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                });
            }
            if (layEvent == "send") {
                layer.load(2);
                $.ajax({
                    url: '/api/Quartz/sendApi/' + data.guid,
                    type: 'post',
                    contentType: "application/json;charset=utf-8",
                    async: true,
                    dataType: 'json',
                    complete: function (obj) {
                        layer.closeAll('loading');
                    },
                    success: function (result) {
                        if (result.success) {
                            layer.msg(result.message, { icon: 1 });
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg(result.message, { icon: 2 });
                        }
                    }
                });
            }
        });

        // 搜索按钮点击事件
        form.on('submit(formSubSearchTbBas)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });

        //开启或者关闭任务
        form.on('switch(openTask)', function (obj) {
            layer.load(2);
            var url = '/api/Quartz/openTask/' + obj.elem.value
            $.ajax({
                url: url,
                type: 'PUT',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({ guid: obj.elem.value, isOpen: obj.elem.checked ? 1 : 0 }),
                async: true,
                dataType: 'json',
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.message, { icon: 1 });
                    } else {
                        layer.msg(result.message, { icon: 2 });
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                }
            });
        });
        //渲染时间控件
        laydate.render({
            elem: '#createTime',
            range: true
        });
        //新增任务
        $("#addTask").click(function () {
            admin.open({
                area: ["900px", "500px"],
                type: 1,
                title: '新任务',
                content: $('#modelAuth').html(),
                success: function (layero, dIndex) {
                    form.render('select');
                    QuaryAuth();
                    form.on('select(isTip)', function (select) {
                        if (select.value == 0) {
                            $(".EmailTip").fadeOut();
                        } else if (select.value == 1) {
                            $(".EmailTip").fadeIn();
                        }
                    });
                    // 表单提交事件
                    form.on('submit(saveTask)', function (rowData) {
                        layer.load(2);
                        $.ajax({
                            url: '/api/Quartz',
                            type: 'post',
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(rowData.field),
                            dataType: "json",
                            async: true,
                            complete: function (obj) {
                                layer.closeAll('loading');
                            },
                            success: function (res) {
                                if (res.success) {
                                    layer.msg(res.message, { icon: 1 });
                                    insTb.reload({}, 'data');
                                    layer.close(dIndex);
                                } else {
                                    layer.msg(res.message, { icon: 2 });
                                }
                            }
                        });
                        return false;
                    });
                }
            });
        });


        //查询验证配置
        function QuaryAuth() {
            layer.load(2);
            $.ajax({
                url: '/api/Quartz/QuaryTaskAuth',
                type: 'get',
                contentType: "application/json;charset=utf-8",
                async: false,
                dataType: 'json',
                complete: function (obj) {
                    layer.closeAll('loading');
                },
                success: function (res) {
                    if (res.success) {
                        var select = document.getElementById('authType');
                        $(res.data).each(function (index, item){
                            var option = document.createElement('option');
                            option.setAttribute('value', item.authGuid);
                            option.innerText = item.authName;
                            select.appendChild(option);
                        });
                        form.render('select');
                    }
                }
            });
        }
    });
</script>

